package org.igeek.atomqq.task;

import java.net.URL;

import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.domain.Friend;
import org.igeek.atomqq.domain.Group;
import org.igeek.atomqq.net.HttpConnection;
import org.igeek.atomqq.net.HttpConnection.Request_TYPE;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.util.URLManager;
import org.igeek.atomqq.util.Utils;


/**
 * 获取验证码
 * 正常返回类型: 正常返回类型: image/[bmp|jpeg|gif]
 * GET image/[bmp|jpeg|gif]	https://face[1-9].qun.qq.com/cgi/svr/face/getface?cache=1&type=1&fid=0&uin=qq号&vfwebqq=令牌&t=1328346730559时间	
 * @author hangxin1940@gmail.com
 * @version 创建时间：2012-1-29 下午1:32:26
 * 
 */
public class FaceTask extends CachedAsyncTask<Void,Void,Void>{
	private TaskResultListener listener;
	private FaceResponse response;
	private long currentUin;
	private long freindUin;
	public final static int FRIEND_FACE=1;
	public final static int GROUP_FACE=4;
	private int faceType;
	private int flag;
	
	private BaseActivity activity;
	
	public FaceTask(BaseActivity activity){
		this.activity=activity;
	}
	
	public void setOnTaskResult(TaskResultListener listener,int flag){
		this.listener=listener;
		this.flag=flag;
	}
	
	public void setParams(long currentUin,long freindUin,int faceType){
		this.currentUin=currentUin;
		this.freindUin=freindUin;
		this.faceType=faceType;
	}

	@Override
	protected Void doInBackground(Void... params) {
		String face="";
		
		
			
		String vfwebqq=HttpConnection.getCookieManager().getCookieValue("vfwebqq");
		
		StringBuffer surl=new StringBuffer(URLManager.GET_FACE_URL());
		surl.append("?cache=0&fid=0&type=");
		surl.append(faceType);
		surl.append("&uin=");
		surl.append(freindUin);
		surl.append("&vfwebqq=");
		surl.append(vfwebqq);
		try {
			URL url = new URL(surl.toString());
			
			HttpConnection conn=new HttpConnection(url, Request_TYPE.GET);
			conn.setReferer(URLManager.REFER_web3_qq_com);
			conn.connect();
			Response res=conn.getResponse();
			
			if(faceType==FRIEND_FACE)
				face=Utils.writeFriendFaceImage(currentUin, freindUin, res.getStream());
			else
				face=Utils.writeGroupFaceImage(currentUin, freindUin, res.getStream());
			conn.disconnect();
			
			
			
		}catch (Exception e) {
			e.printStackTrace();
			face="";
			
		}
		response=new FaceResponse();
		
		if(faceType==FRIEND_FACE){
			Friend friend=new Friend(freindUin);
			friend.setUin(freindUin);
			friend.setFace(face);
			response.setFace(friend);
		}else{
			Group group=new Group(freindUin);
			group.setCode(freindUin);
			group.setFace(face);
			response.setGroup(group);
		}
		
		return null;
	}

	
	@Override
	protected void onPostExecute(Void result) {
		if(null!=response)
			listener.onTaskResult(response,flag);
	}	

	public class FaceResponse extends Response{
		private Friend face;
		private Group group;

		public void setFace(Friend face) {
			this.face = face;
		}

		public Friend getFace() {
			return face;
		}

		public void setGroup(Group group) {
			this.group = group;
		}

		public Group getGroup() {
			return group;
		}
	}

}
